﻿'*******************************************************************
' Program: Data Center Visio Rack Tool
' Author: Albert E Edlund
' Date:
'
' Purpose:
'
'
'*******************************************************************


Imports Microsoft.Practices.EnterpriseLibrary.Common
Imports Microsoft.Practices.EnterpriseLibrary.Data
Imports Microsoft.Practices.EnterpriseLibrary.ExceptionHandling
Imports Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Logging


Imports System.Data
Imports System.IO
Imports System.Xml



Imports DC_Rack.Objects.clsDC_RackData

Public Class clsTopoPoint


#Region " Properties and definitions "


    ' this is where we store the data that we want to put into a table
    Private m_colActivePoints As Collection(Of dc_TopoPoint)

    Public m_db As Database
    Public m_ds As DataSet
    Public m_dt As DataTable

    Public m_oc As Collection(Of dc_TopoPoint)
    Public m_dc As dc_TopoPoint

    Public m_deleteCommand As DbCommand
    Public m_insertCommand As DbCommand
    Public m_updateCommand As DbCommand


    Public Const readAll As String = "selecttblTopoPoint"


    Property _colTopoPoints() As Collection(Of dc_TopoPoint)
        Get
            If m_oc Is Nothing Then
                m_oc = New Collection(Of dc_TopoPoint)
            End If
            Return Me.m_oc
        End Get
        Set(ByVal value As Collection(Of dc_TopoPoint))
            If m_oc Is Nothing Then
                m_oc = New Collection(Of dc_TopoPoint)
            End If
            Me.m_oc = value
        End Set
    End Property

    Property _colActivePoints() As Collection(Of dc_TopoPoint)
        Get
            If m_colActivePoints Is Nothing Then
                m_colActivePoints = New Collection(Of dc_TopoPoint)
            End If
            _colActivePoints = m_colActivePoints
        End Get
        Set(ByVal value As Collection(Of dc_TopoPoint))
            If m_colActivePoints Is Nothing Then
                m_colActivePoints = New Collection(Of dc_TopoPoint)
            End If
            m_colActivePoints = value
        End Set
    End Property

    Private m_parentform As Object
    Public Property _ParentForm() As Object
        Get
            Return Me.m_parentform
        End Get
        Set(ByVal value As Object)
            Me.m_parentform = value
        End Set
    End Property


    Private Sub initProperties()


    End Sub

#End Region

#Region " Move/Transform Data "

    '
    ' create columns
    '
    Public Sub CreateTableColumns(ByRef tempTable As DataTable)

        Dim newDC As dc_TopoPoint = New dc_TopoPoint

        Try

            Dim colTopoPointPortDataKey As New System.Data.DataColumn
            colTopoPointPortDataKey.ColumnName = "TopoPointPortDataKey"
            colTopoPointPortDataKey.DataType = GetType(Integer)
            colTopoPointPortDataKey.DefaultValue = newDC.constTopoPointPortDataKey
            tempTable.Columns.Add(colTopoPointPortDataKey)

            Dim colTopoPointLinks As New System.Data.DataColumn
            colTopoPointLinks.ColumnName = "TopoPointLinks"
            colTopoPointLinks.DataType = GetType(String)
            colTopoPointLinks.DefaultValue = newDC.constTopoPointLinks
            tempTable.Columns.Add(colTopoPointLinks)

            Dim colTopoPointLinkCt As New System.Data.DataColumn
            colTopoPointLinkCt.ColumnName = "TopoPointLinkCt"
            colTopoPointLinkCt.DataType = GetType(Integer)
            colTopoPointLinkCt.DefaultValue = newDC.constTopoPointLinkCt
            tempTable.Columns.Add(colTopoPointLinkCt)

            Dim colTopoPointLink As New System.Data.DataColumn
            colTopoPointLink.ColumnName = "TopoPointLink"
            colTopoPointLink.DataType = GetType(Integer)
            colTopoPointLink.DefaultValue = newDC.constTopoPointLink
            tempTable.Columns.Add(colTopoPointLink)

            Dim colTopoPointBuildDate As New System.Data.DataColumn
            colTopoPointBuildDate.ColumnName = "TopoPointBuildDate"
            colTopoPointBuildDate.DataType = GetType(Date)
            colTopoPointBuildDate.DefaultValue = newDC.constTopoPointBuildDate
            tempTable.Columns.Add(colTopoPointBuildDate)

            newDC = Nothing

        Catch sql As SqlException
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(sql, "Database Policy")
            If (rethrow) Then
                Throw
            End If
        Catch ex As Exception
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(ex, "Log Only Policy")
            If (rethrow) Then
                Throw
            End If
        End Try

        If newDC IsNot Nothing Then
            newDC = Nothing
        End If


    End Sub


    '
    ' use for writing records
    '
    Public Sub movePropertiesToDataRow _
            (ByRef tempRow As DataRow, _
             ByVal tempDC As dc_TopoPoint)

        Try

            With tempDC
                tempRow.SetField("TopoPointPortDataKey", .topopointPortDataKey)
                tempRow.SetField("TopoPointLinks", .topopointLinks)
                tempRow.SetField("TopoPointLinkCt", .topopointLinkCt)
                tempRow.SetField("TopoPointLink", .topopointLink)
                tempRow.SetField("TopoPointBuildDate", .topopointBuildDate)
            End With



        Catch sql As SqlException
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(sql, "Database Policy")
            If (rethrow) Then
                Throw
            End If
        Catch ex As Exception
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(ex, "Log Only Policy")
            If (rethrow) Then
                Throw
            End If
        End Try

    End Sub


    '
    ' for reading records
    '
    Public Sub moveDataRowToProperties _
            (ByVal tempRow As DataRow)

        Try

            With m_dc
                .initProperties()
                .topopointPortDataKey = tempRow.Item("TopoPointPortDataKey")
                .topopointLinks = tempRow.Item("TopoPointLinks")
                .topopointLinkCt = tempRow.Item("TopoPointLinkCt")
                .topopointLinkCt = tempRow.Item("TopoPointLink")
                .topopointBuildDate = tempRow.Item("TopoPointBuildDate")
            End With


        Catch sql As SqlException
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(sql, "Database Policy")
            If (rethrow) Then
                Throw
            End If
        Catch ex As Exception
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(ex, "Log Only Policy")
            If (rethrow) Then
                Throw
            End If
        End Try

    End Sub


    '
    ' for reading records
    '
    Public Sub moveDataRowToProperties _
            (ByVal tempRow As DataRow, _
             ByRef tempDC As dc_TopoPoint)

        Try

            With tempDC
                .initProperties()
                .topopointPortDataKey = tempRow.Item("TopoPointPortDataKey")
                .topopointLinks = tempRow.Item("TopoPointLinks")
                .topopointLinkCt = tempRow.Item("TopoPointLinkCt")
                .topopointLinkCt = tempRow.Item("TopoPointLink")
                .topopointBuildDate = tempRow.Item("TopoPointBuildDate")
            End With


        Catch sql As SqlException
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(sql, "Database Policy")
            If (rethrow) Then
                Throw
            End If
        Catch ex As Exception
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(ex, "Log Only Policy")
            If (rethrow) Then
                Throw
            End If
        End Try

    End Sub

    '
    ' for moving active table to collection for wpf
    '
    Public Sub moveDataTableToObservableCollection _
            (ByVal dtTemp As DataTable)

        Try

            If 0 < dtTemp.Rows.Count Then

                If m_oc IsNot Nothing Then
                    m_oc = Nothing
                    m_oc = New Collection(Of dc_TopoPoint)
                Else
                    m_oc = New Collection(Of dc_TopoPoint)
                End If

                Dim drTemp As DataRow
                Dim dcTemp As dc_TopoPoint
                For Each drTemp In dtTemp.Rows
                    dcTemp = New dc_TopoPoint
                    moveDataRowToProperties(drTemp, dcTemp)
                    m_oc.Add(dcTemp)
                Next

            End If

        Catch ex As Exception
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(ex, "Log Only Policy")
            If (rethrow) Then
                Throw
            End If
        End Try

    End Sub

    '
    '
    '
    Public Function moveDataTableToXML() As XmlDocument

        Dim writer = New System.IO.StringWriter
        Dim m_dt_xml As Xml.XmlDocument = Nothing

        Try


            m_dt.WriteXml(writer, XmlWriteMode.WriteSchema, True)
            m_dt_xml = Nothing
            m_dt_xml = New Xml.XmlDocument
            m_dt_xml.LoadXml(writer.ToString)

        Catch ex As Exception
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(ex, "Log Only Policy")
            If (rethrow) Then
                Throw
            End If
        Finally

            writer = Nothing

        End Try

        Return m_dt_xml


    End Function




#End Region

#Region " Standard Database Calls "

    '
    '
    '
    Private Sub buildInsertTopoPointCommand()
        Try

            m_insertCommand = m_db.GetStoredProcCommand("insertTopoPoint")
            Dim cmdActive As DbCommand = m_insertCommand

            m_db.AddInParameter(cmdActive, "TopoPointPortDataKey", DbType.Int32, "TopoPointPortDataKey", DataRowVersion.Current)
            m_db.AddInParameter(cmdActive, "TopoPointLinks", DbType.String, "TopoPointLinks", DataRowVersion.Current)
            m_db.AddInParameter(cmdActive, "TopoPointLinkCt", DbType.Int16, "TopoPointLinkCt", DataRowVersion.Current)
            m_db.AddInParameter(cmdActive, "TopoPointLink", DbType.Int32, "TopoPointLink", DataRowVersion.Current)
            m_db.AddInParameter(cmdActive, "TopoPointBuildDate", DbType.Date, "TopoPointBuildDate", DataRowVersion.Current)


        Catch sql As SqlException
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(sql, "Database Policy")
            If (rethrow) Then
                Throw
            End If
        End Try
    End Sub

    '
    '
    '
    Private Sub buildUpdateTopoPointCommand()
        Try


            m_updateCommand = m_db.GetStoredProcCommand("updateTopoPoint")
            Dim cmdActive As DbCommand = m_updateCommand

            m_db.AddInParameter(cmdActive, "TopoPointPortDataKey", DbType.Int32, "TopoPointPortDataKey", DataRowVersion.Current)
            m_db.AddInParameter(cmdActive, "TopoPointLinks", DbType.String, "TopoPointLinks", DataRowVersion.Current)
            m_db.AddInParameter(cmdActive, "TopoPointLinkCt", DbType.Int16, "TopoPointLinkCt", DataRowVersion.Current)
            m_db.AddInParameter(cmdActive, "TopoPointLink", DbType.Int32, "TopoPointLink", DataRowVersion.Current)
            m_db.AddInParameter(cmdActive, "TopoPointBuildDate", DbType.Date, "TopoPointBuildDate", DataRowVersion.Current)


        Catch sql As SqlException
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(sql, "Database Policy")
            If (rethrow) Then
                Throw
            End If
        End Try

    End Sub

    '
    '
    '
    Private Sub buildDeleteTopoPointCommand()
        Try


            m_deleteCommand = m_db.GetStoredProcCommand("deleteTopoPoint")
            Dim cmdActive As DbCommand = m_deleteCommand

            m_db.AddInParameter(cmdActive, "TopoPointPortDataKey", DbType.Int32, "TopoPointPortDataKey", DataRowVersion.Current)

        Catch sql As SqlException
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(sql, "Database Policy")
            If (rethrow) Then
                Throw
            End If
        End Try
    End Sub

    '
    '
    '
    Public Function dbUpdateDataSet() As Int16

        Dim rowsAffected As Int16 = 0

        Try

            rowsAffected = m_db.UpdateDataSet(m_ds, dc_racktables.tbltopopoint.tostring, _
                                        m_insertCommand, _
                                        m_updateCommand, _
                                        m_deleteCommand, _
                                        UpdateBehavior.Standard)
            'msgbox rowsAffected

        Catch sql As SqlException
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(sql, "Database Policy")
            If (rethrow) Then
                Throw
            End If
        Catch ex As Exception
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(ex, "Log Only Policy")
            If (rethrow) Then
                Throw
            End If
        End Try

        Return rowsAffected

    End Function


    '
    ' create a record based on property values move it to the generic datatable
    ' then copy the table to the database
    '
    Public Function addTopoPointSourceRecord _
            (ByVal tempDC As dc_TopoPoint, _
             Optional ByVal blnSilent As Boolean = False) _
             As Integer

        Dim intRowsAffected As Integer = 0

        Try

            initDataSetAndTable()

            Dim m_dr As DataRow = m_ds.Tables(dc_racktables.tbltopopoint.tostring).NewRow
            movePropertiesToDataRow(m_dr, tempDC)
            m_ds.Tables(dc_racktables.tbltopopoint.tostring).Rows.Add(m_dr)
            intRowsAffected = dbUpdateDataSet()

            If blnSilent = False Then
                MsgBox("DataRowsUpated = " & intRowsAffected)
            End If

        Catch sql As SqlException
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(sql, "Database Policy")
            If (rethrow) Then
                Throw
            End If
        Catch ex As Exception
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(ex, "Log Only Policy")
            If (rethrow) Then
                Throw
            End If
        End Try

        Return intRowsAffected

    End Function


    '
    ' clear the general dataset and table, create columns in the table, and move it to
    ' the dataset
    '
    Public Sub initDataSetAndTable()

        Try

            ' make sure the root stuff is there
            If m_ds Is Nothing Then
                m_ds = New DataSet
            Else
                m_ds = Nothing
                m_ds = New DataSet
            End If
            m_ds.SchemaSerializationMode = SchemaSerializationMode.IncludeSchema

            If m_dt Is Nothing Then
                m_dt = New DataTable
            Else
                m_dt = Nothing
                m_dt = New DataTable
            End If

            If m_oc Is Nothing Then
                m_oc = New Collection(Of dc_TopoPoint)
            Else
                m_oc = Nothing
                m_oc = New Collection(Of dc_TopoPoint)
            End If

            If m_dc Is Nothing Then
                m_dc = New dc_TopoPoint
            Else
                m_dc = Nothing
                m_dc = New dc_TopoPoint
            End If

            CreateTableColumns(m_dt)
            m_dt.TableName = dc_racktables.tbltopopoint.tostring

            m_ds.Tables.Add(m_dt)


        Catch sql As SqlException
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(sql, "Database Policy")
            If (rethrow) Then
                Throw
            End If
        Catch ex As Exception
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(ex, "Log Only Policy")
            If (rethrow) Then
                Throw
            End If
        End Try

    End Sub


#End Region

#Region " Database Calls  "

    '
    '
    '
    Private Sub dbDeleteTempTopoPoints()

        Try

            Dim cmd As DbCommand = m_db.GetStoredProcCommand("deleteTblTempTopoPoint")
            Dim deletedCount As Integer = m_db.ExecuteNonQuery(cmd)

        Catch sql As SqlException
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(sql, "Database Policy")
            If (rethrow) Then
                Throw
            End If
        End Try


    End Sub

    '
    '
    '
    Private Sub dbInsertTempTopoPointSideA()

        Try


            Dim cmd As DbCommand = m_db.GetStoredProcCommand("insertTempTopoPointSideA")
            Dim deletedCount As Integer = m_db.ExecuteNonQuery(cmd)

        Catch sql As SqlException
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(sql, "Database Policy")
            If (rethrow) Then
                Throw
            End If
        End Try


    End Sub

    '
    '
    '
    Private Sub dbInsertTempTopoPointSideB()


        Try

            Dim cmd As DbCommand = m_db.GetStoredProcCommand("insertTempTopoPointSideB")
            Dim deletedCount As Integer = m_db.ExecuteNonQuery(cmd)

        Catch sql As SqlException
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(sql, "Database Policy")
            If (rethrow) Then
                Throw
            End If
        End Try


    End Sub

    '
    '
    '
    Private Sub dbSelectTempTopoPoints()


        Try

            initDataSetAndTable()

            Dim tempitemsTable As String = "tblTempTopoPoint"
            ' Retrieve the initial data
            Dim cmd As DbCommand = m_db.GetStoredProcCommand("selectTempTopoPoints")
            m_db.LoadDataSet(cmd, m_ds, tempitemsTable)

            m_dt = m_ds.Tables(tempitemsTable)


            Dim intFound As Integer = 0
            Dim intDups As Integer = 0

            Dim m_dr As DataRow
            Dim TopoPoint As dc_TopoPoint
            'Dim TopoPointTemp As dc_TopoPoint
            Dim colLinks As Collection = New Collection
            Dim colTempLinks As Collection = New Collection


            Dim intRowCt As Integer = m_dt.Rows.Count
            Dim intCt As Integer = 0

            If Not (m_parentform Is Nothing) Then
                m_parentform.lblOperation.Text = "Move TopoPoints to Collection for Analysis"
            End If
            For Each m_dr In m_dt.Rows

                intCt += 1

                If Not (m_parentform Is Nothing) Then
                    m_parentform.lblStatus.Text = "Item Point Row " & intCt & " of " & intRowCt
                End If

                ' handle it as a new entry
                TopoPoint = Nothing
                TopoPoint = New dc_TopoPoint
                colLinks = Nothing
                colLinks = New Collection

                TopoPoint.topopointPortDataKey = m_dr.Item("TopoPointPortDataKey")
                colLinks.Add(m_dr.Item("TopoPointLinkKey"))
                TopoPoint._colLinks = colLinks


                'If _colTopoPoints.ExistsPort(TopoPoint) = False Then

                '    _colTopoPoints.Add(TopoPoint)

                'Else

                '    Dim currPtr As Int32
                '    TopoPointTemp = Nothing
                '    TopoPointTemp = New dc_TopoPoint
                '    colTempLinks = Nothing
                '    colTempLinks = New Collection
                '    currPtr = _colTopoPoints.IndexOfPort(TopoPoint)
                '    TopoPointTemp = _colTopoPoints.GetItem(currPtr)
                '    colTempLinks = TopoPointTemp.m_links
                '    If memberLongOfCollection(colTempLinks, m_dr.Item("TopoPointLinkKey")) = False Then
                '        colTempLinks.Add(m_dr.Item("TopoPointLinkKey"))
                '        ' update the links in the new entry
                '        TopoPoint.m_links = colTempLinks
                '        ' add thets updated TopoPoint
                '        _colTopoPoints.Replace(currPtr, TopoPoint)
                '    End If
                'End If


            Next




        Catch ex As Exception
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(ex, "Log Only Policy")
            If (rethrow) Then
                Throw
            End If
        End Try


    End Sub

    '
    '
    '
    Public Sub dbInsertTopoPointCollection()




        Try
            Dim intX As Integer
            Dim intCt As Integer
            intCt = -1

            _ParentForm.lblStatus.Text = "Saving Item Points to Database"

            initDataSetAndTable()

            ' Retrieve the initial data
            Dim dbCommand As DbCommand
            dbCommand = m_db.GetStoredProcCommand(readall)
            m_db.LoadDataSet(dbCommand, m_ds, dc_racktables.tbltopopoint.tostring)

            m_dt = m_ds.Tables(dc_racktables.tbltopopoint.tostring)

            Dim newRow As DataRow
            For intX = 0 To intCt - 1
                m_dc = _colTopoPoints.Item(intX)
                If Not (m_dc Is Nothing) Then
                    newRow = m_dt.NewRow
                    movePropertiesToDataRow(newRow, m_dc)
                    m_dt.Rows.Add(newRow)
                End If
            Next intX

            Dim rowsAffected As Integer = m_db.UpdateDataSet(m_ds, dc_racktables.tbltopopoint.tostring, _
                                                m_insertCommand, _
                                                m_updateCommand, _
                                                m_deleteCommand, _
                                                UpdateBehavior.Standard)

            ' finally do the port analysis
            createPortAnalysis()

        Catch sql As SqlException
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(sql, "Database Policy")
            If (rethrow) Then
                Throw
            End If
        Catch ex As Exception
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(ex, "Log Only Policy")
            If (rethrow) Then
                Throw
            End If
        End Try

    End Sub

    '
    '
    '
    Public Sub dbSelectTopoPoints()


        Try


            initDataSetAndTable()

            ' Retrieve the initial data
            Dim cmd As DbCommand = m_db.GetStoredProcCommand(readAll)
            m_db.LoadDataSet(cmd, m_ds, dc_racktables.tbltopopoint.tostring)
            m_dt = m_ds.Tables(0)

            moveDataTableToObservableCollection(m_dt)

        Catch ex As Exception
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(ex, "Log Only Policy")
            If (rethrow) Then
                Throw
            End If
        End Try


    End Sub

    '
    ' delete the working points table data
    '
    Private Sub dbDeleteTopoPoints()

        Try

            Dim cmd As DbCommand = m_db.GetStoredProcCommand("deletetblTopoPoint")
            Dim deletedCount As Integer = m_db.ExecuteNonQuery(cmd)

        Catch sql As SqlException
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(sql, "Database Policy")
            If (rethrow) Then
                Throw
            End If
        End Try

    End Sub



#End Region

#Region "Verify Item Points"


    '
    ' test one is to verify whether we have any item points with more than
    ' two cables connected
    '
    Public Sub dbSelectTopoPointMultiLinks()


        Try

            initDataSetAndTable()

            Dim TempTopoPointTable As String = "tblTempTopoPoint"
            ' Retrieve the initial data
            Dim cmd As DbCommand = m_db.GetStoredProcCommand("selectTopoPointMultiLinks")
            m_db.LoadDataSet(cmd, m_ds, TempTopoPointTable)

            m_dt = m_ds.Tables(TempTopoPointTable)

        Catch ex As Exception
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(ex, "Log Only Policy")
            If (rethrow) Then
                Throw
            End If
        End Try



    End Sub

    '
    ' test to verify whether we have any active item points with more than
    ' one cables connected
    '
    Public Sub dbSelectTopoPointMultiActiveLinks()




        Try

            initDataSetAndTable()

            Dim TempTopoPointTable As String = "tblTempTopoPoint"
            ' Retrieve the initial data
            Dim cmd As DbCommand = m_db.GetStoredProcCommand("selectTopoPointMultiActiveLinks")
            m_db.LoadDataSet(cmd, m_ds, TempTopoPointTable)

            m_dt = m_ds.Tables(TempTopoPointTable)

        Catch ex As Exception
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(ex, "Log Only Policy")
            If (rethrow) Then
                Throw
            End If
        End Try


    End Sub


#End Region

#Region "Discover Helpers"

    Public Sub dbSelectTopoPointsActivePoints()



        Try

            Dim m_dt_active As DataTable = New DataTable
            Dim activeTable As String = "tblTopoPointActive"
            ' Retrieve the initial data
            Dim cmd As DbCommand = m_db.GetStoredProcCommand("selectTopoPointActivePoints")
            m_db.LoadDataSet(cmd, m_ds, activeTable)

            'moveDataTableToItemCollection(m_ds, activeTable, m_colActivePoints)



        Catch ex As Exception
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(ex, "Log Only Policy")
            If (rethrow) Then
                Throw
            End If
        End Try




    End Sub

#End Region

#Region " Port Analysis "

    '
    '
    '
    Public Sub createPortAnalysis()

        Try

            'Dim cmd As DbCommand = m_db.GetStoredProcCommand("deleteTblPortAnalysis")
            'Dim deletedCount As Integer = m_db.ExecuteNonQuery(cmd)

            'cmd = m_db.GetStoredProcCommand("insertPAPossiblePorts")
            'Dim insertedCount As Integer = m_db.ExecuteNonQuery(cmd)

            'cmd = m_db.GetStoredProcCommand("updatePAInstalledPorts")
            'Dim updatedCount As Integer = m_db.ExecuteNonQuery(cmd)


            'Debug.Print(deletedCount & " " & insertedCount & " " & updatedCount)

        Catch sql As SqlException
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(sql, "Database Policy")
            If (rethrow) Then
                Throw
            End If
        End Try



    End Sub

    '
    '
    '
    Public Sub dbSelectTopoPointActiveByRackKey(ByVal intRackKey As Int32)

        Try

            initDataSetAndTable()


            ' Retrieve the initial data
            Dim cmd As DbCommand = m_db.GetStoredProcCommand("selectTopoPointActiveByRackKey")
            m_db.AddInParameter(cmd, "@RackKey", DbType.Int32, intRackKey)
            m_db.LoadDataSet(cmd, m_ds, dc_racktables.tbltopopoint.tostring)

            m_dt = m_ds.Tables(dc_racktables.tbltopopoint.tostring)


        Catch ex As Exception
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(ex, "Log Only Policy")
            If (rethrow) Then
                Throw
            End If
        End Try






    End Sub




#End Region

#Region " Methods "

    '
    '
    '
    Public Sub buildPointsCollection()

        Try



            Dim strMsg As String = ""

            If m_oc Is Nothing Then
                m_oc = New Collection(Of dc_TopoPoint)
            Else
                m_oc = Nothing
                m_oc = New Collection(Of dc_TopoPoint)
            End If


            ' clear the points table
            strMsg = "start delete item points"
            dbDeleteTopoPoints()
            dbDeleteTemptopopoints()
            ' first get the side a item points
            dbInsertTemptopopointSideA()
            ' then get the side b item points
            dbInsertTemptopopointSideB()
            ' now read the temp table
            dbSelectTempTopoPoints()

            dbDeleteTemptopopoints()

        Catch sql As SqlException
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(sql, "Database Policy")
            If (rethrow) Then
                Throw
            End If
        Catch ex As Exception
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(ex, "Log Only Policy")
            If (rethrow) Then
                Throw
            End If
        End Try
    End Sub

    '
    '
    '
    Private Function memberLongOfCollection _
            (ByVal colLongs As Collection, _
            ByVal lngMember As Int32) _
            As Boolean

        Dim intX As Integer
        Dim blnReturn As Boolean
        blnReturn = False

        For intX = 1 To colLongs.Count
            If lngMember = colLongs.Item(intX) Then
                blnReturn = True
                GoTo ExitHandler
            End If
        Next intX

ExitHandler:
        memberLongOfCollection = blnReturn

    End Function



#End Region


#Region " Constructor "


    Public Sub New()

        m_db = DatabaseFactory.CreateDatabase()

        initDataSetAndTable()

        buildDeleteTopoPointCommand()
        buildInserttopopointCommand()
        buildUpdateTopoPointCommand()

    End Sub

#End Region


End Class
